﻿using Flurl.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp生产订单开工
{
    public partial class Form1 : Form
    {
     
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            //设置日期控件默认值为当前月第一天
            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            //string StartDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
            //string EndDate = dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd");
            //string data = "StartDate=" + StartDate + "&EndDate=" + EndDate;
            ////通过flurl发送get请求
            //string result = await (url+"?"+data).GetStringAsync();

            string sql = @"select mm_pmo.dbilldate 单据日期,mm_pmo.vbillcode 单据号,mm_pmo.billmaker 制单人,
                            CASE
                            WHEN mm_mo.cdeptid LIKE '1001A110000000V585T3' THEN '1线'
                            WHEN mm_mo.cdeptid LIKE '1001A110000000V58DLM' THEN '2线'
                            ELSE mm_mo.cdeptid
                            END AS 产线,
                            mm_mo.cmaterialid,bd_material.code 物料号,bd_material.name 物料名称,materialspec AS 规格,
                                            materialtype AS 型号,mm_mo.nastnum 数量
                            from mm_pmo  join mm_mo on  mm_mo.cpmohid = mm_pmo.CPMOHID 
                            left join bd_material on bd_material.pk_material = mm_mo.cmaterialid
                            where mm_mo.dr=0  and mm_pmo.pk_org = '0001A11000000000Q5NJ'";
            //如果选了日期，就加日期查询
            if (dateTimePicker1.Value != null)
            {
                sql += $" and mm_pmo.dbilldate >= '{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' ";
            }
            if (dateTimePicker2.Value != null)
            {
                sql += $" and mm_pmo.dbilldate < '{dateTimePicker2.Value.AddDays(1).ToString("yyyy-MM-dd")}' ";
            }
            
            
            DataTable dt = SqlSugarHelp.scope.GetConnection("Oracle").Ado.GetDataTable(sql);

            string sql2 = @"SELECT vbillcode FROM  `mm_pmo`
                                union ALL
                                SELECT vbillcode FROM  `mm_pmo_his`
                                union ALL
                                SELECT vbillcode FROM  `mm_pmo二线`
                                union ALL
                                SELECT vbillcode FROM  `mm_pmo_his二线`
";


            if (radioButton已投产.Checked == true)
            {
                List<string> billcodes = SqlSugarHelp.scope.GetConnection("MySql").Ado.SqlQuery<string>(sql2);
                //dt中只保留已投产的数据
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    if (!billcodes.Contains(dt.Rows[i]["单据号"].ToString()))
                    {
                        dt.Rows.RemoveAt(i);
                    }
                }

            }

            if (radioButton未投产.Checked == true)
            {
                List<string> billcodes = SqlSugarHelp.scope.GetConnection("MySql").Ado.SqlQuery<string>(sql2);
                //dt中只保留未投产的数据
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    if (billcodes.Contains(dt.Rows[i]["单据号"].ToString()))
                    {
                        dt.Rows.RemoveAt(i);
                    }
                }
            }


            this.dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (e.RowIndex >= 0 && dataGridView1.Columns[e.ColumnIndex].Name == "投产")
            {
                //获取当前行的 单据号
                string vbillcode = this.dataGridView1.Rows[e.RowIndex].Cells["单据号"].Value.ToString();
                string 产线 = this.dataGridView1.Rows[e.RowIndex].Cells["产线"].Value.ToString();
                //提示是否确定投产
                DialogResult dr = MessageBox.Show($"是否确定{vbillcode}投产到{产线}？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    string url = "";
                    if (产线 == "1线")
                    {
                        url = "http://115.231.66.30:23001/MM_PMO/Add";
                        //调用接口
                        ReturnMsg result = url.PostJsonAsync(new { lineno = 1, vbillcode = vbillcode }).ReceiveJson<ReturnMsg>().Result;
                        //转为json对象
                        MessageBox.Show(result.MessageString);
                    }
                    else if (产线 == "2线")
                    {
                        url = "http://115.231.66.30:23001/MM_PMO/Add2";
                        //调用接口
                        ReturnMsg result = url.PostJsonAsync(new { lineno = 2, vbillcode = vbillcode }).ReceiveJson<ReturnMsg>().Result;
                        //转为json对象
                        MessageBox.Show(result.MessageString);
                    }
                    else
                    {
                        MessageBox.Show($"产线只有一线二线，当前产线{产线}不存在");
                        return;
                    }
                  
                    

                }
            }
        }
    }
}
